import {mapGetters, mapActions} from 'vuex'

const statusMap = [{ label: '失效', value: '0', color: 'c_error' }, { label: '有效', value: '1', color: 'c_primary' }]

const dictMixin = {
  computed: {
    ...mapGetters(['dict', 'orgAndDediceTree', 'busiList', 'orgTree', 'orgAllTree'])
  },
  filters: {
    enableFilter (flag) {
      return flag ? '正常' : '禁用'
    },
    statusFilter (val) {
      return val ? statusMap.find(x => x.value === val.toString()).label : ''
    },
    statusTypeFilter (val) {
      return val ? statusMap.find(x => x.value === val.toString()).color : ''
    },
    enableTypeFilter (flag) {
      return flag ? 'c_primary' : 'c_error'
    }
  },
  methods: {
    ...mapActions(['getDict' ,'getOrgAndDeviceTree', 'getbusiList', 'getOrgTree', 'getOrgAllTree']),
    textFilter (val, field) {
      if (val) {
        if (!this.dict[field]) {
          this.getDict(field)
        }
        const options = this.dict[field] || []
        const item = options.find(x => {
          if (x.value === val.toString()) {
            return x
          }
        })
        return item ? item.label : val
      } else {
        return ''
      }
    }
  }
}

export default dictMixin
